import {
    Component,
    Injector,
    ViewEncapsulation,
    ViewChild,
    ElementRef,
    OnInit
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    ComboboxItemDto,
    SideTaskServiceProxy,
    ReceiveTaskInput,
    WorkingTimeServiceProxy,
    NodeWorkerForPipleinOutput
} from '@shared/service-proxies/service-proxies';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import * as moment from 'moment';
import { finalize } from 'rxjs/operators';
import { PutIntoProductionModalComponent } from '@app/plan/maintasks/put-into-production-modal.component';
import { MenuItem } from 'primeng/api';
import { FileDownloadService } from '@shared/utils/file-download.service';

@Component({
    templateUrl: './taskworking.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class TaskworkingComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    dateRange: Date[];
    advancedFiltersAreShown = false;
    work: NodeWorkerForPipleinOutput[];
    en: any;
    filterText = '';

    constructor(
        injector: Injector,
        private _workingTimeServiceProxy: WorkingTimeServiceProxy,
        private _fileDownloadService: FileDownloadService
    ) {
        super(injector);
    }

    ngOnInit() {
        // this.en = {
        //   firstDayOfWeek: 0,
        //   monthNames: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        //   monthNamesShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        //   dateFormat: 'yy-mm',
        //   weekHeader: 'Wk'
        // };
    }
    getWorking(event?: LazyLoadEvent) {
        console.log(this.dateRange);
        let start = null;
        let end = null;
        if (this.dateRange !== undefined) {
            start = moment(this.dateRange[0]);
            end = moment(this.dateRange[1]).add(1, 'day');
        }
        this._workingTimeServiceProxy
            .getPipelineWorkers(
                this.filterText,
                '',
                start,
                end,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(
                    this.paginator,
                    event
                ),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    remWorking() {
        this.dateRange = undefined;
        this.getWorking();
    }
    exportToExcel(): void {
        let start = null;
        let end = null;
        if (this.dateRange !== undefined) {
            start = moment(this.dateRange[0]);
            end = moment(this.dateRange[1]).add(1, 'day');
        }
        this._workingTimeServiceProxy
            .getWorkTimeToExcel(start, end)
            .subscribe(result => {
                this._fileDownloadService.downloadTempFile(result);
            });
    }
}
